<script>
$(document).ready(function(){
	{params}
	
	home = {
		tabsarr:{},
		nowtabs:false,
		cnum:'',
		opentabs:[],
		resize:function(){
			var mhei = viewheight;
			$('#mainleft').css('height',''+mhei+'px');
			var _wl = 230;
			if(get('mainleft').style.display=='none')_wl=0;
			var mwei = winWb()-_wl-1;
			$('#tabsindexm').css('width',''+mwei+'px');
			var hei =  mhei- $('#tabsindexm').height();
			$('#indexcontent').css({width:''+mwei+'px',height:''+hei+'px'});
			
			homeheight = hei;
			homewidth  = mwei;
			this.changhhhsv();
		},
		init:function(){
			if(js.web.ismobile())$('#mainleft').hide();
			if(params.cnum)this.cnum = params.cnum;
			this.resize();
			
			js.plugin.scrollbar(function(){
				$('#mainleft').perfectScrollbar('destroy');
				$('#mainleft').perfectScrollbar();
			});
			if(adminface)get('myface').src=adminface;
			$('#hometitle').html(params.title);
			this.objcont = $('#content_allmainview');
			this.objtabs = $('#tabs_title');
			
			if(!this.cnum){
				js.ajax.get({
					url:'api/home/data',
					loading:'初始加载...',
				}, function(ret){
					home.initback(ret.data);
				});
			}else{
				var str 	= js.getoption('companyinfo'+this.cnum+'');
				if(str)this.showcompanyinfo(js.decode(str));
				js.ajax.get({
					url:'api/company/data?hmode=0',
					cnum:this.cnum,
					loading:'初始加载...',
				}, function(ret){
					home.initbackcompany(ret.data);
				});
			}
		},
		initback:function(da){
			this.alldata = da;
			var face = da.userinfo.face;
			js.setoption('userinfo',da.userinfo);
			js.setoption('rockfile',da.rockfile);
			
			js.setoption('systemtitle',da.systemtitle);
			systemtitle = da.systemtitle;
			js.setoption('adminface',face);
			if(get('myface') && face)get('myface').src=face;
			$('#hometitle').html(systemtitle);
			xcy.setTitle(systemtitle);
			
			this.menuarr = da.menuarr;
			this.showmenu('0','menulist',1);
			this.addtabs({icons:'home',num:'home',hideclose:true,name:'首页',url:'admin/index'});
		},
		initbackcompany:function(da){
			this.alldata = da;
			js.setoption('companyinfo'+this.cnum+'', da.companyinfo);
			js.setoption('useainfo'+this.cnum+'', da.useainfo);
			this.showcompanyinfo(da.companyinfo);
			var face = da.userinfo.face;
			js.setoption('userinfo',da.userinfo);
			js.setoption('rockfile',da.rockfile);
			js.setoption('adminface',face);
			if(get('myface') && face)get('myface').src=face;
			js.setoption('systemtitle',da.systemtitle);
			
			var biaoti = da.companyinfo.name;
			$('#hometitle').html(biaoti);
			xcy.setTitle(biaoti);
			
			if(usertype==2){
			}
			this.menuarr  = da.menuarr;
			this.agentarr = da.agentarr;
			var funids = this.chuliagent(da.agentarr);
			
			this.showmenu('0','menulist',1);
			this.addtabs({icons:'home',num:'home',hideclose:true,name:'首页',url:'company/homeme',params:{cnum:this.cnum,funids:funids}});
			
			var reim = da.reim;
			if(reim && reim.url){
				reim.id   = da.userinfo.id;
				reim.name = da.useainfo.name;
				importjs('websocket','create',reim);
			}
		},
		showcompanyinfo:function(info){
			var logo = 'images/nologo.png';
			if(info.logo)logo = info.logo;
			get('iconindex').href = logo;
			get('logo').src = logo;
			this.companyinfo = info;
			js.setoption('ico_'+this.cnum+'', logo);
		},
		chuliagent:function(da){
			var i,d,len=da.length,ds=[],aob={},lx,j,str='';
			for(i=0;i<len;i++){
				d = da[i];
				lx= d.type;
				if(!aob[lx])aob[lx]=[];
				d.xu = i;
				d.isagent = 1;
				aob[lx].push(d);
				
				if(str)str+=',';
				str+=d.id;
			}
			j = 0;
			for(lx in aob){
				j++;
				this.menuarr.push({name:lx,id:'-'+j+'',pid:'0',icons:this.mtypeicons(lx)});
				da = aob[lx];
				len= da.length;
				for(i=0;i<len;i++){
					d = da[i];
					d.pid = '-'+j+'';
					d.fuid= d.id+'';
					d.id  = 'm'+d.id;
					if(!d.icons)d.icons='images/noimg.jpg';
					this.menuarr.push(d);
				}
			}
			return str;
		},
		mtypeicons:function(lx){
			var da = this.alldata.mtypeicon;
			if(!da)return '';
			for(var i=0;i<da.length;i++){
				if(da[i].name==lx)return da[i].value;
			}
			return '';
		},
		openagent:function(i){
			var d = this.agentarr[i];
			var bh = d.mknum,url=d.url;
			if(bh){
				var cans = {num:bh+this.cnum+d.url,name:d.name,url:'flow/list',params:{num:bh,cnum:this.cnum,otype:'company',funitid:d.fuid,pnum:d.url}};
				this.addtabs(cans);
			}else{
				if(url){
					window.open(url);
				}else{
					js.show.msg('未设置打开地址');
				}
			}
		},
		showmenu:function(pid,view,lev){
			var s = '',da=this.menuarr,len=da.length,i,d,ico,sto;
			for(i=0;i<len;i++){
				d = da[i];
				if(d.pid==pid){
					if(typeof(d.downshu)=='undefined')d.downshu = this.menustotal(d.id);
					sto = d.downshu;
					this.menuarr[i].downshu = sto;
					s+='<div style="padding-left:'+(lev*15)+'px" class="menu-item" onclick="home.menuclick('+i+','+lev+',this)">';
					ico = d.icons;
					if(!ico)ico = 'bookmark-empty';
					if(ico.indexOf('/')>-1){
						s+='<img src="'+ico+'" height="20px" align="absmiddle" class="radius5" width="20px"> ';
					}else{
						s+='<i class="icon-'+ico+'"></i> ';
					}
					s+=''+d.name+'';
					if(sto>0)s+='<font id="fontangle'+d.id+'"><i class="icon-angle-down"></i></font>';
					s+='</div>';
				}
			}
			$('#'+view+'').html(s);
		},
		menuclick:function(oi,lv,o1){
			var d = this.menuarr[oi];
			if(d.downshu>0){
				var view = 'menulist_'+d.id+'',fview='#fontangle'+d.id+'';
				$(fview).html('<i class="icon-angle-up"></i>');
				if(!get(view)){
					$(o1).after('<div id="'+view+'"></div>');
					this.showmenu(d.id, view, lv+1);
				}else{
					$('#'+view+'').toggle();
					if(get(view).style.display=='none')$(fview).html('<i class="icon-angle-down"></i>');
				}
			}else{
				this.menutogo(d);
			}
		},
		menutogo:function(d){
			var bh = d.num,arr,url,params={},i,a1,a2;
			if(d.isagent==1){
				this.openagent(d.xu);
				return;
			}
			if(!bh)bh='menu'+d.id+'';
			if(!d.url){js.show.error('无菜单地址');return;}
			arr = d.url.split('?');
			url = arr[0];
			if(arr[1]){
				a1 = arr[1].split('&');
				for(i=0;i<a1.length;i++){
					a2 = a1[i].split('=');
					params[a2[0]]=a2[1];
				}
			}
			if(this.cnum){
				params.cnum  = this.cnum;
				params.otype = 'company';
			}
			var cans = {num:bh.replace('{cnum}',this.cnum),name:d.name,icons:d.icons,url:url,params:params};
			this.addtabs(cans);
			//xcy.openkey(cans);//新窗口
		},
		menustotal:function(pid){
			var da=this.menuarr,len=da.length,i,to=0;
			for(i=0;i<len;i++){
				if(da[i].pid==pid)to++;
			}
			return to;
		},
		hidemenu:function(o1){
			if(get('mainleft').style.display=='none'){
				o1.innerHTML='<i class="icon-indent-left"></i>';
			}else{
				o1.innerHTML='<i class="icon-indent-right"></i>';
			}
			$('#mainleft').toggle();
			this.resize();
		},
		clickuser:function(){
			this.addtabs({icons:'user',num:'geren',name:'个人中心',url:'admin/geren', params:{cnum:this.cnum}});
		},
		addtabs:function(a){
			var num = a.num,url=a.url;
			if(url.indexOf('open:')==0){window.open(url.substr(5));return;}
			
			this.nowtabs = a;
			if(this.changetabs(num))return true;
			
			var s = '<li temp="tabs" oncontextmenu="return home.rightmenu(\''+num+'\', this, event)" nowrap onclick="home.changetabs(\''+num+'\',1)" id="tabs_'+num+'" class="active"><font>';
			if(a.icons)s+='<i class="icon-'+a.icons+'"></i>  ';
			s+=a.name+'</font>';
			if(!a.hideclose)s+='<span onclick="home.closetabs(\''+num+'\')"><img src="images/del.png" width="12" height="12"></span>';
			s+='</li>';
			this.objtabs.append(s);
			this.changhhhsv();
			
			this.objcont.append('<div temp="content" style="margin:10px;" id="content_'+num+'"><div align="center" style="padding-top:50px" ><i style="height:40px;width:40px" class="rock-loading"></i></div></div>');
			xcy.addcont(url, 'content_'+num+'', a.params);
			
			this.tabsarr[num] = a;
		},
		rightmenu:function(num,o,e){
			var da = [],d = this.tabsarr[num];
			if(!d.hideclose)da.push({name:'关闭',lx:0});
			da.push({name:'新窗口打开',lx:1});
			importjs('menu','create', {
				obj:$(o),
				showXY:{left:e.clientX+1,top:e.clientY+1},
				data:da,
				onclick:function(d1){
					if(d1.lx==0)home.closetabs(num);
					if(d1.lx==1)xcy.openkey(d);
				}
			});
			return false;
		},
		changetabs:function(num,lx){
			if(this.coloebool)return;
			if(!lx)lx=0;
			$("div[temp='content']").hide();
			$("[temp='tabs']").removeClass();
			var bo = false;
			if(get('content_'+num+'')){
				$('#content_'+num+'').show();
				$('#tabs_'+num+'').addClass('active');
				this.nowtabs = this.tabsarr[num];
				if(typeof(this.nowtabs.onshow)=='function')this.nowtabs.onshow();
				bo = true;
			}
			this.opentabs.push(num);
			if(lx==0)this.changhhhsv(num);
			return bo;
		},
		closetabs:function(num){
			this.tabsarr[num] = false;
			$('#content_'+num+'').remove();
			$('#tabs_'+num+'').remove();
			if(num == this.nowtabs.num){
				var now ='home',i,noux;
				for(i=this.opentabs.length-1;i>=0;i--){
					noux= this.opentabs[i];
					if(get('content_'+noux+'')){
						now = noux;
						break;
					}
				}
				this.changetabs(now);
			}
			this.coloebool = true;
			this.changhhhsv();
			setTimeout('home.coloebool=false',10);
		},
		changhhhsv:function(num){
			var mwd = $('#tabsindexm').width();
			var obj = $('#tabs_title').find('li'),sw1=0,len=obj.length;
			if(len>4)for(var i=0;i<len;i++){
				if(obj[i].id!='moretabs')sw1+=obj[i].offsetWidth;
			}
			if(sw1>=mwd){
				$('#moretabs').show();
			}else{
				$('#moretabs').hide();
			}
		},
		moretabs:function(o1){
			this.showmoretabs(o1)
		},
		showmoretabs:function(o1){
			var da = [],nu,st1,d1;
			for(nu in this.tabsarr){
				d1 = this.tabsarr[nu];
				if(d1){
					st1 = d1.name;
					if(d1.icons)st1='<i class="icon-'+d1.icons+'"></i> '+st1+'';
					if(nu==this.nowtabs.num)st1='<b style="color:'+maincolor+'">'+st1+'</b>';
					da.push({num:nu,name:st1});
				}
			}
		},
		search:function(){
			js.modal.prompt({
				msg:'搜索菜单',
				inputmsg:'菜单名称/菜单编号',
				title:'搜索'
			},function(jg,txt){
				if(jg=='yes' && txt){
					js.show.error('未找到('+txt+')相关');
				}
			});
		}
	}
	
	js.initbtn(home);
	
	xcy.resieze = function(){
		home.resize();
	}
});
</script>
<link rel="stylesheet" href="res/theme/base/home.css"/>
<div style="display:inline-block">
	<div id="mainleft" class="mainleft" style="width:230px;float:left;overflow:hidden;position:relative">
		<div class="maintitle">
			<img src="images/logo.png" onclick="js.viewimg(this)" class="radius5" id="logo" style="display:inline;" align="absmiddle" height="24" width="24"> <font id="hometitle">title</font>
		</div>
		<div id="menulist"></div>
	</div>
	
	<div style="float:left;">
		<div id="tabsindexm" class="tabsindex">
			<ul id="tabs_title" style="padding:0px;margin:0;height:60px;overflow:hidden">
				<li clickevt="hidemenu"><i class="icon-indent-left"></i></li>
				<li clickevt="search" style="display:none"><i class="icon-search"></i></li>
				<li style="float:right;" clickevt="clickuser"><img style="width:18px;height:18px;border-radius:50%" id="myface" src="images/noface.png" align="absmiddle"></li>
				<li id="moretabs" clickevt="moretabs" style="float:right;display:none"><i class="icon-th-list"></i> 选择 <i class="icon-caret-down"></i> </li>
			</ul>
		</div>
		
		<div id="indexcontent" style="overflow:auto;position:relative;">
			<div id="content_allmainview"></div>
		</div>
	</div>
</div>